import { createApp } from 'vue'
import router from './router/index'
import './style.css'
import App from './App.vue'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import store from './store/index'
import PanelHead from './components/panelHead.vue'


// 刷新后的动态路由添加
const localData = localStorage.getItem('pz_v3pz')
if (localData) {
  store.commit('dynamicMenu', JSON.parse(localData).menu.routerList)
  store.state.menu.routerList.forEach(item => {
    router.addRoute('main', item)
  })
}


// 路由导航守卫  全局前置守卫
router.beforeEach((to, from) => {
  const token = localStorage.getItem('pz_token')
  // 非登录界面并且token不存在时
  if (!token && to.path !== '/login') {
    return '/login'
  } else if (token && to.path === '/login') {
    return '/'
  } else {
    return true
  }
})



const app = createApp(App)
// icon 图标全局注册
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
// 注册为全局组件
app.component('PanelHead', PanelHead)
// 挂载路由
app.use(router)
// store 挂载
app.use(store)
app.mount('#app')
